package br.com.jk.util.parametro;

import br.com.jk.util.Util;
import java.awt.Frame;
import java.awt.event.KeyEvent;

/**
 *
 * @author camilo
 */
public class ParametroDataDialog extends javax.swing.JDialog {

  private ParametroData parametro;

  /**
   * Permite criar um form de acordo a solicitação do parâmetro por período ou uma única data
   *
   * @param parent Formulário que faz a chamada do método construtor
   * @param modal Se somente a tela do parâmetro da data puder ser modificado no momento escolha
   * true
   * @param periodo Se o parâmetro solicitado para o usuário será por período defina true.
   * @param parametro
   */
  public ParametroDataDialog(Frame parent, boolean modal, boolean periodo, ParametroData parametro) {
    super(parent, modal);
    initComponents();

    this.parametro = parametro;

    if (!periodo) {
      jlDataFinal.setVisible(false);
      jlDataFinal.setEnabled(false);
      jftfDataFinal.setVisible(false);
      jftfDataFinal.setVisible(false);
    }
  }

  /**
   * Creates new form ParametroDataDialog
   *
   * @param parent
   * @param modal
   */
  public ParametroDataDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
  }

  /**
   * This method is called from within the constructor to initialize the form. WARNING: Do NOT
   * modify this code. The content of this method is always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jlDataInicial = new javax.swing.JLabel();
    jftfDataInicial = new javax.swing.JFormattedTextField();
    jlDataFinal = new javax.swing.JLabel();
    jftfDataFinal = new javax.swing.JFormattedTextField();
    jbSelect = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Informe a Data");
    setModal(true);
    setResizable(false);
    setType(java.awt.Window.Type.UTILITY);

    jlDataInicial.setFont(new java.awt.Font("Liberation Sans", 1, 12)); // NOI18N
    jlDataInicial.setText("Data Inicial");

    try {
      jftfDataInicial.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
    } catch (java.text.ParseException ex) {
      ex.printStackTrace();
    }
    jftfDataInicial.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    jftfDataInicial.setFont(new java.awt.Font("Liberation Sans", 1, 12)); // NOI18N
    jftfDataInicial.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        jftfDataInicialKeyPressed(evt);
      }
    });

    jlDataFinal.setFont(new java.awt.Font("Liberation Sans", 1, 12)); // NOI18N
    jlDataFinal.setText("Data Final");

    try {
      jftfDataFinal.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
    } catch (java.text.ParseException ex) {
      ex.printStackTrace();
    }
    jftfDataFinal.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    jftfDataFinal.setFont(new java.awt.Font("Liberation Sans", 1, 12)); // NOI18N
    jftfDataFinal.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        jftfDataFinalKeyPressed(evt);
      }
    });

    jbSelect.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/jk/imagens/dialog-ok.png"))); // NOI18N
    jbSelect.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jbSelectActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addComponent(jlDataInicial)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jftfDataInicial, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addGroup(layout.createSequentialGroup()
            .addComponent(jlDataFinal)
            .addGap(18, 18, 18)
            .addComponent(jftfDataFinal, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jbSelect, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jbSelect, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
              .addComponent(jlDataInicial)
              .addComponent(jftfDataInicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
              .addComponent(jlDataFinal)
              .addComponent(jftfDataFinal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
    setLocationRelativeTo(null);
  }// </editor-fold>//GEN-END:initComponents

  private void jbSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSelectActionPerformed
    this.getParametro();
    this.dispose();
  }//GEN-LAST:event_jbSelectActionPerformed

  private void jftfDataInicialKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jftfDataInicialKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
      jftfDataFinal.requestFocus();
    }

  }//GEN-LAST:event_jftfDataInicialKeyPressed

  private void jftfDataFinalKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jftfDataFinalKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
      jbSelect.requestFocus();
    }
  }//GEN-LAST:event_jftfDataFinalKeyPressed

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
          javax.swing.UIManager.setLookAndFeel(info.getClassName());
          break;
        }
      }
    } catch (ClassNotFoundException ex) {
      java.util.logging.Logger.getLogger(ParametroDataDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
      java.util.logging.Logger.getLogger(ParametroDataDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
      java.util.logging.Logger.getLogger(ParametroDataDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
      java.util.logging.Logger.getLogger(ParametroDataDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the dialog */
    java.awt.EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        ParametroDataDialog dialog = new ParametroDataDialog(new javax.swing.JFrame(), true);
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
          @Override
          public void windowClosing(java.awt.event.WindowEvent e) {
            System.exit(0);
          }
        });
        dialog.setVisible(true);
      }
    });
  }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton jbSelect;
  private javax.swing.JFormattedTextField jftfDataFinal;
  private javax.swing.JFormattedTextField jftfDataInicial;
  private javax.swing.JLabel jlDataFinal;
  private javax.swing.JLabel jlDataInicial;
  // End of variables declaration//GEN-END:variables

  public ParametroData getParametro() {
    parametro.setDataInicial(Util.formatDataSToD(jftfDataInicial.getText()));
    parametro.setDataFinal(Util.formatDataSToD(jftfDataFinal.getText()));
    return parametro;
  }
}
